package com.kaikai.edu.controller;


import com.kaikai.commonutils.R;
import com.kaikai.edu.entity.EduChapter;
import com.kaikai.edu.entity.chapter.ChapterVo;
import com.kaikai.edu.service.EduChapterService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author yujiankai
 * @since 2021-05-14
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {
    @Autowired
    EduChapterService eduChapterService;

    //根据courseId查询章节和小节并返回List<Chapter>对象
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> chapterVoList = eduChapterService.getChapterVideo(courseId);

        return R.ok().data("list", chapterVoList);
    }

    //根据章节id查询
    @ApiOperation(value = "根据章节id查询章节")
    @GetMapping("getChapterInfo/{id}")
    public R getChapterInfo(@PathVariable String id) {
        EduChapter eduChapter = eduChapterService.getById(id);
        return R.ok().data("chapter", eduChapter);
    }

    @ApiOperation(value = "添加章节")
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter chapter) {
        try {
            eduChapterService.save(chapter);
            return R.ok();
        } catch (Exception e) {
            return R.error().message("添加失败");
        }


    }

    @ApiOperation(value = "修改章节")
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter chapter) {
        eduChapterService.updateById(chapter);
        return R.ok();
    }

    @ApiOperation(value = "删除章节")
    @DeleteMapping("{id}")
    public R deleteChapterById(@PathVariable String id) {

            eduChapterService.deleteChapter(id);
            return R.ok();


    }

}

